<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>jni-java | ccplus</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../java/jni-android.html" />
    
    
    <link rel="prev" href="../java/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.1"
        data-chapter-title="jni-java"
        data-filepath="java/jni-java.md"
        data-basepath=".."
        data-revision="Fri May 13 2016 17:02:07 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        目录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="Android/index.html">
            
                
                    <a href="../Android/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Android
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="Android/touchEvent.html">
            
                
                    <a href="../Android/touchEvent.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        触摸点击事件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="Android/screenAdaptation.html">
            
                
                    <a href="../Android/screenAdaptation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        屏幕适配
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Android/android4.0.html">
            
                
                    <a href="../Android/android4.0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        4.0
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c/index.html">
            
                
                    <a href="../c/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        c
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c/Hello.html">
            
                
                    <a href="../c/Hello.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Hello
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c/DataType.html">
            
                
                    <a href="../c/DataType.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c/operator.html">
            
                
                    <a href="../c/operator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="c/array.html">
            
                
                    <a href="../c/array.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        数组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c/pointer.html">
            
                
                    <a href="../c/pointer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        指针
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="c/variables.html">
            
                
                    <a href="../c/variables.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        变量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="c/statement.html">
            
                
                    <a href="../c/statement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="c/file.html">
            
                
                    <a href="../c/file.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="c/structure.html">
            
                
                    <a href="../c/structure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        结构体
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="c/function.html">
            
                
                    <a href="../c/function.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.</b>
                        
                        函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="c/multisource.html">
            
                
                    <a href="../c/multisource.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.11.</b>
                        
                        多个源文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="c/gdb.html">
            
                
                    <a href="../c/gdb.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.12.</b>
                        
                        gdb调试
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="linux/index.html">
            
                
                    <a href="../linux/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Linux
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="linux/command/index.html">
            
                
                    <a href="../linux/command/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        命令
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c++/index.html">
            
                
                    <a href="../c++/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        C++
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c++/section1.html">
            
                
                    <a href="../c++/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        第一节
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c++/section2.html">
            
                
                    <a href="../c++/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        第二节
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="java/index.html">
            
                
                    <a href="../java/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        java
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="5.1" data-path="java/jni-java.html">
            
                
                    <a href="../java/jni-java.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        jni-java
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="java/jni-android.html">
            
                
                    <a href="../java/jni-android.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        jni-android
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="gitbook/index.html">
            
                
                    <a href="../gitbook/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        gitbook
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="gitbook/installation.html">
            
                
                    <a href="../gitbook/installation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="gitbook/commands.html">
            
                
                    <a href="../gitbook/commands.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        命令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="gitbook/syntax.html">
            
                
                    <a href="../gitbook/syntax.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="gitbook/settings.html">
            
                
                    <a href="../gitbook/settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="gitbook/plugins.html">
            
                
                    <a href="../gitbook/plugins.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        插件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="gitbook/bookjson.html">
            
                
                    <a href="../gitbook/bookjson.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        json
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >ccplus</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="jnijava">jni-java</h1>
<h4 id="java&#x672C;&#x5730;&#x65B9;&#x6CD5;&#x901A;&#x8FC7;jni&#x8FDE;&#x63A5;&#x81F3;c&#x51FD;&#x6570;">java&#x672C;&#x5730;&#x65B9;&#x6CD5;&#x901A;&#x8FC7;jni&#x8FDE;&#x63A5;&#x81F3;c&#x51FD;&#x6570;</h4>
<p>1). &#x7F16;&#x5199;java&#x4EE3;&#x7801;.HelloJni.java</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HelloJni</span> </span>{
    <span class="hljs-comment">// &#x672C;&#x5730;&#x65B9;&#x6CD5;&#x58F0;&#x660E;</span>
    <span class="hljs-function"><span class="hljs-keyword">native</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printHello</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">native</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printString</span><span class="hljs-params">(String str)</span></span>;
    <span class="hljs-comment">//&#x52A0;&#x8F7D;&#x5E93;</span>
    <span class="hljs-keyword">static</span> {
        System.loadLibrary(<span class="hljs-string">&quot;hellojni&quot;</span>);
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        HelloJni helloJni=<span class="hljs-keyword">new</span> HelloJni();
        <span class="hljs-comment">//&#x8C03;&#x7528;&#x672C;&#x5730;&#x65B9;&#x6CD5;,&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x8C03;&#x7528;&#x7684;&#x662F;&#x4F7F;&#x7528;c&#x8BED;&#x8A00;&#x7F16;&#x5199;&#x7684;JNI&#x672C;&#x5730;&#x51FD;&#x6570;</span>
        helloJni.printHello();
        helloJni.printString(<span class="hljs-string">&quot;&#x5728;java&#x4E2D;&#x8C03;&#x7528;c&quot;</span>);
    }
}
</code></pre>
<p>2). &#x7F16;&#x8BD1;java&#x4EE3;&#x7801;<br>&#x4F7F;&#x7528;&#x547D;&#x4EE4;:javac HelloJni.java,&#x751F;&#x6210;HelloJni.class&#x6587;&#x4EF6;&#x3002;<br>3).  &#x751F;&#x6210;c&#x8BED;&#x8A00;&#x5934;&#x6587;&#x4EF6;.HelloJni.h<br>&#x4F7F;&#x7528;&#x547D;&#x4EE4;:javah HelloJni,&#x5176;&#x4E2D;HelloJni&#x662F;&#x4E0A;&#x4E00;&#x6B65;&#x751F;&#x6210;&#x7684;class&#x6587;&#x4EF6;&#x7684;&#x540D;&#x5B57;&#x3002;<br>4).  &#x7F16;&#x5199;c/c++&#x4EE3;&#x7801;&#x3002;hellojni.c&#x6587;&#x4EF6;      </p>
<pre><code class="lang-c"><span class="hljs-preprocessor">#<span class="hljs-keyword">include</span> <span class="hljs-string">&quot;HelloJNI.h&quot;</span></span>
<span class="hljs-preprocessor">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-comment">/**
 * Java_HelloJNI_printHelJo()&#x51FD;&#x6570;&#x5BF9;&#x5E94;&#x4E8E;HelloJNI &#x7C7B;&#x4E2D;&#x7684;printHello()&#x672C;&#x5730;&#x65B9;&#x6CD5;&#xFF0C;
 *&#x7528;&#x4E8E;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x8F93;&#x51FA;&quot; Hello World ! &quot;&#x3002;
 * ***/</span>
<span class="hljs-function">JNIEXPORT <span class="hljs-keyword">void</span> JNICALL <span class="hljs-title">Java_HelloJni_printHello</span>
<span class="hljs-params">(JNIEnv * env, jobject obj)</span> </span>{
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Hello World!\n&quot;</span>);
    <span class="hljs-keyword">return</span>;
};

<span class="hljs-comment">/****
 *Java_HelloJN1_printString()&#x51FD;&#x6570;&#x63A5;&#x6536;printString()&#x672C;&#x5730;&#x65B9;&#x6CD5;&#x4F20;&#x9012;&#x8FC7;&#x6765;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x53C2;
 *&#x6570;&#xFF0C;&#x5E76;&#x8F93;&#x51FA;&#x5230;&#x63A7;&#x5236;&#x53F0;&#x4E0A;&#x3002;printString()&#x672C;&#x5730;&#x65B9;&#x6CD5;&#x4F20;&#x9012;&#x8FC7;&#x6765;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4FDD;&#x5B58;&#x5728;
 *Java_HelloJNI_printString()&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;string (&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x4E3A;jstring) &#x4E2D;&#x3002;
 *&#x5982;&#x524D;&#x6240;&#x8FF0;&#xFF0C; jstring &#x662F;Java &#x672C;&#x5730;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x5BF9;&#x5E94;&#x4E8E;Java &#x4E2D;&#x7684;String &#x7C7B;&#x578B;&#xFF0C; &#x5728;&#x5185;&#x5B58;&#x4E2D;
 *&#x5360;&#x7528;16 &#x4F4D;&#x3002;&#x800C;C &#x8BED;&#x8A00;&#x4E2D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4EC5;&#x5360;&#x7528;8 &#x4F4D;&#xFF0C;&#x5728;C &#x8BED;&#x8A00;&#x4E2D;&#x65E0;&#x6CD5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;jstring
 *&#x7C7B;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C; &#x56E0;&#x6B64;&#x9700;&#x8981;&#x5C06;jstring &#x7C7B;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;C &#x8BED;&#x8A00;&#x5B57;&#x7B26;&#x4E32;&#x3002;
 *GetStringUTFChars()&#x662F;JNI &#x51FD;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x5C06;Java &#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;C &#x8BED;&#x8A00;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x4F7F;
 *&#x7528;&#x6B64;&#x51FD;&#x6570;&#x8F6C;&#x6362;&#x5B57;&#x7B26;&#x4E32;&#x4E4B;&#x540E;&#xFF0C;&#x518D;&#x4F7F;&#x7528;printf()&#x51FD;&#x6570;&#x8F93;&#x51FA;&#xFF0C; &#x5373;&#x53EF;&#x5C06;&#x672C;&#x5730;&#x65B9;&#x6CD5;&#x4F20;&#x9012;&#x8FC7;&#x6765;
 *&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8F93;&#x51FA;&#x5230;&#x63A7;&#x5236;&#x53F0;&#x4E0A;&#x3002;
 *
 *
 * ****/</span>
<span class="hljs-function">JNIEXPORT <span class="hljs-keyword">void</span> JNICALL <span class="hljs-title">Java_HelloJni_printString</span>
<span class="hljs-params">(JNIEnv * env, jobject obj, jstring jstring)</span> </span>{
    <span class="hljs-comment">//&#x5C06;java String &#x8F6C;&#x5316;&#x4E3A;C&#x5B57;&#x7B26;&#x4E32;</span>
    <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *str=(*env)-&gt;GetStringUTFChars(env,jstring,<span class="hljs-number">0</span>);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s!\n&quot;</span>,str);
    <span class="hljs-keyword">return</span>;
};
</code></pre>
<p>5). &#x751F;&#x6210;c&#x5171;&#x4EAB;&#x5E93;&#x3002;hellojni.dll<br>&#x4F7F;&#x7528;&#x547D;&#x4EE4;:
gcc -Wall -D<em>JNI_IMPLEMENTATION</em> -Wl,--kill-at -Id:/java/include -Id:/java/include/win32 -shared hellojni.c -o hellojni.dll<br>6). &#x8FD0;&#x884C;java&#x7A0B;&#x5E8F;.<br>&#x4F7F;&#x7528;&#x547D;&#x4EE4;:java HelloJni</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../java/index.html" class="navigation navigation-prev " aria-label="Previous page: java"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../java/jni-android.html" class="navigation navigation-next " aria-label="Next page: jni-android"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
